package com.softline.documentsmall.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.softline.documentsmall.dao.*;
import com.softline.documentsmall.entity.Dictionary;
import com.softline.documentsmall.entity.*;
import com.softline.documentsmall.service.DictionaryService;
import com.softline.documentsmall.utils.tool;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.nlp.v20190408.NlpClient;
import com.tencentcloudapi.nlp.v20190408.models.SimilarWordsRequest;
import com.tencentcloudapi.nlp.v20190408.models.SimilarWordsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.*;

/**
 * @author zhongweicheng
 * @date 2020/9/9 9:54
 */
@Service
@Slf4j
public class DictionaryServiceImp implements DictionaryService {

    @Autowired
    DictionaryMapper mapper;

    @Autowired
    EntryDetailMapper detailMapper;

    @Autowired
    ClassificationMapper classificationMapper;

    @Autowired
    LogAnalyseMapper logAnalyseMapper;

    @Autowired
    LogAnalyseServiceImpl logAnalyseServiceImpl;

    @Autowired
    RelateBookMapper relateBookMapper;

    @Autowired
    RelateJournalArticalMapper relateJournalArticalMapper;
    @Autowired
    DictionaryKeywordConfigMapper dictionaryKeywordConfigMapper;

    public static final String[] languageType = {"en", "cn", "ar", "fr", "de", "it", "jp", "kr", "pt", "ru", "es"};

    @Override
    public List<Map<String, Object>> selectList(Integer id, String title, Integer pageNo, Integer pageSize) {
        List<Dictionary> dictionaryList = mapper.selectList(id, title, pageSize * (pageNo - 1), pageSize);
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (dictionaryList != null && !ObjectUtils.isEmpty(dictionaryList)) {
            dictionaryList.forEach(item -> {
                JSONObject tempObject = new JSONObject();
                tempObject.put("id", item.getId());
                tempObject.put("name", item.getNameCn());
                JSONArray tempArray = new JSONArray();
                if (item.getAudioEn() != null && !"".equals(item.getAudioEn())) {
                    tempArray.add("en");
                }
                if (item.getAudioAr() != null && !"".equals(item.getAudioAr())) {
                    tempArray.add("ar");
                }
                if (item.getAudioCn() != null && !"".equals(item.getAudioCn())) {
                    tempArray.add("cn");
                }
                if (item.getAudioDe() != null && !"".equals(item.getAudioDe())) {
                    tempArray.add("de");
                }
                if (item.getAudioEs() != null && !"".equals(item.getAudioEs())) {
                    tempArray.add("es");
                }
                if (item.getAudioFr() != null && !"".equals(item.getAudioFr())) {
                    tempArray.add("fr");
                }
                if (item.getAudioIt() != null && !"".equals(item.getAudioIt())) {
                    tempArray.add("it");
                }
                if (item.getAudioJp() != null && !"".equals(item.getAudioJp())) {
                    tempArray.add("jp");
                }
                if (item.getAudioKr() != null && !"".equals(item.getAudioKr())) {
                    tempArray.add("kr");
                }
                if (item.getAudioPt() != null && !"".equals(item.getAudioPt())) {
                    tempArray.add("pt");
                }
                if (item.getAudioRu() != null && !"".equals(item.getAudioRu())) {
                    tempArray.add("ru");
                }
                tempObject.put("languageList", tempArray);
                resultList.add(tempObject);
            });
        }
        return resultList;
    }

    @Override
    public JSONObject selectById(int id) {
        Dictionary dictionary = mapper.selectByPrimaryKey(id);
        List<Map<String, Object>> list = new ArrayList<>();
        JSONObject resultJsonObject = new JSONObject();
        if (dictionary != null && !ObjectUtils.isEmpty(dictionary)) {
            //英文
            Map<String, Object> en = new HashMap<>();
            en.put("type", "en");
            en.put("name", dictionary.getNameEn());
            en.put("audio", dictionary.getAudioEn());
            list.add(en);
            //中文
            Map<String, Object> cn = new HashMap<>();
            cn.put("type", "cn");
            cn.put("name", dictionary.getNameCn());
            cn.put("audio", dictionary.getAudioCn());
            list.add(cn);
            //阿拉伯语
            Map<String, Object> ar = new HashMap<>();
            ar.put("type", "ar");
            ar.put("name", dictionary.getNameAr());
            ar.put("audio", dictionary.getAudioAr());
            list.add(ar);
            //法语
            Map<String, Object> fr = new HashMap<>();
            fr.put("type", "fr");
            fr.put("name", dictionary.getNameFr());
            fr.put("audio", dictionary.getAudioFr());
            list.add(fr);
            //德语
            Map<String, Object> de = new HashMap<>();
            de.put("type", "de");
            de.put("name", dictionary.getNameDe());
            de.put("audio", dictionary.getAudioDe());
            list.add(de);
            //意大利语
            Map<String, Object> it = new HashMap<>();
            it.put("type", "it");
            it.put("name", dictionary.getNameIt());
            it.put("audio", dictionary.getAudioIt());
            list.add(it);
            //日本语
            Map<String, Object> jp = new HashMap<>();
            jp.put("type", "jp");
            jp.put("name", dictionary.getNameJp());
            jp.put("audio", dictionary.getAudioJp());
            list.add(jp);
            //韩语
            Map<String, Object> kr = new HashMap<>();
            kr.put("type", "kr");
            kr.put("name", dictionary.getNameKr());
            kr.put("audio", dictionary.getAudioKr());
            list.add(kr);
            //葡萄牙语
            Map<String, Object> pt = new HashMap<>();
            pt.put("type", "pt");
            pt.put("name", dictionary.getNamePt());
            pt.put("audio", dictionary.getAudioPt());
            list.add(pt);
            //俄语
            Map<String, Object> ru = new HashMap<>();
            ru.put("type", "ru");
            ru.put("name", dictionary.getNameRu());
            ru.put("audio", dictionary.getAudioRu());
            list.add(ru);
            //西班牙语
            Map<String, Object> es = new HashMap<>();
            es.put("type", "es");
            es.put("name", dictionary.getNameEs());
            es.put("audio", dictionary.getAudioEs());
            list.add(es);
            resultJsonObject.put("id", dictionary.getId());
            resultJsonObject.put("languageList", list);
        }
        return resultJsonObject;
    }

    @Override
    public int insert(Dictionary dictionary) {
        return mapper.insertSelective(dictionary);
    }

    @Override
    public int update(Dictionary dictionary) {
        return mapper.updateByPrimaryKeySelective(dictionary);
    }

    @Override
    public Long selectLocalCount(Integer id, String title) {
        return mapper.selectLocalCount(id, title);
    }

    @Override
    public Long selectThumbsUpCount(String type) {
        return mapper.selectThumbsUpCount(type);
    }

    /**
     * @param type：en cn ar fr de it jp kr pt ru es
     * @return
     */
    @Override
    public List<IndexInitVo> indexInitByLanguageType(String type) {
        List<IndexInitVo> value = Lists.newArrayList();
        if (type == null)
            return value;
        //先获取最大id，确定范围
        Integer maxId = mapper.findMaxId();
        List<Dictionary> list = Lists.newArrayList();
        //循环取数 取五条数据即可
        while (list.size() != 5) {
            Integer id = (int) (1 + Math.random() * (maxId - 1 + 1));
            Long disCount = list.stream().filter(r -> r.getId() == id).count();
            if (disCount != 0) {
                //重新循环获取,确保不重复
                continue;
            }
            Dictionary dictionary = mapper.findById(id);
            //确保id有数据
            if (dictionary != null) {
                list.add(dictionary);
            }
        }
        //按照类型获取
        list.stream().forEach(r -> {
            Integer id = r.getId();
            IndexInitVo vo = new IndexInitVo();
            vo.setId(id);
            // en cn ar fr de it jp kr pt ru es
            if ("en".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameEn());
            } else if ("cn".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameCn());
            } else if ("ar".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameAr());
            } else if ("fr".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameFr());
            } else if ("de".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameDe());
            } else if ("it".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameIt());
            } else if ("jp".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameJp());
            } else if ("kr".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameKr());
            } else if ("pt".equalsIgnoreCase(type)) {
                vo.setValue(r.getNamePt());
            } else if ("ru".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameRu());
            } else if ("es".equalsIgnoreCase(type)) {
                vo.setValue(r.getNameEs());
            } else {
                //默认就是英文
                vo.setValue(r.getNameEn());
            }
            value.add(vo);
        });
        return value;
    }

    @Override
    public IsProfessionalTermVo isProfessional(String value) {
        Dictionary dictionary = null;
        //前端不传递类型 for循环去匹配这几个语种
        for (String type : languageType) {
            dictionary = mapper.isProfessional(type, value);
            //找到满足的就跳出
            if (dictionary != null) {
                break;
            }
        }
        IsProfessionalTermVo vo = new IsProfessionalTermVo();
        if (dictionary != null) {
            vo.setIsProfessionalTerm(true);
            vo.setDictionaryId(dictionary.getId());
        } else {
            vo.setIsProfessionalTerm(false);
            vo.setDictionaryId(null);
        }
        return vo;
    }

    @Override
    public JSONObject search(String type, String value, Integer pageNo, Integer pageSize) {
        //value特殊处理 中间空格替换成%
        value = value.trim().replaceAll(" ", "%");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", pageNo);
        jsonObject.put("size", pageSize);
        jsonObject.put("totals", mapper.searchCount(type, value));
        List<IndexDictionaryVo> searchList = mapper.search(type, value, pageSize * (pageNo - 1), pageSize);
        jsonObject.put("dataList", searchList);
        return jsonObject;
    }

    @Override
    public List<DicVO> listByThumbsUpRanking(Integer pageNo, Integer pageSize, String type) {
        List<DictionaryVO> dictionaryVOList = mapper.listByThumbsUpRanking((pageNo - 1) * pageSize, pageSize, type);
        List<DicVO> result = new ArrayList<>();
        for (int i = 0; i < dictionaryVOList.size(); i++) {
            List<DictionaryAudioDetailVo> detailVos = new ArrayList<>();
            //数据预处理
            DictionaryVO dictionaryVO = dictionaryVOList.get(i);
            //英文
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> en = new HashMap<>();
            en.put("type", "en");
            en.put("name", dictionaryVO.getNameEn());
            en.put("audio", dictionaryVO.getAudioEn());
            list.add(en);
            //中文
            Map<String, Object> cn = new HashMap<>();
            cn.put("type", "cn");
            cn.put("name", dictionaryVO.getNameCn());
            cn.put("audio", dictionaryVO.getAudioCn());
            list.add(cn);
            //阿拉伯语
            Map<String, Object> ar = new HashMap<>();
            ar.put("type", "ar");
            ar.put("name", dictionaryVO.getNameAr());
            ar.put("audio", dictionaryVO.getAudioAr());
            list.add(ar);
            //法语
            Map<String, Object> fr = new HashMap<>();
            fr.put("type", "fr");
            fr.put("name", dictionaryVO.getNameFr());
            fr.put("audio", dictionaryVO.getAudioFr());
            list.add(fr);
            //德语
            Map<String, Object> de = new HashMap<>();
            de.put("type", "de");
            de.put("name", dictionaryVO.getNameDe());
            de.put("audio", dictionaryVO.getAudioDe());
            list.add(de);
            //意大利语
            Map<String, Object> it = new HashMap<>();
            it.put("type", "it");
            it.put("name", dictionaryVO.getNameIt());
            it.put("audio", dictionaryVO.getAudioIt());
            list.add(it);
            //日本语
            Map<String, Object> jp = new HashMap<>();
            jp.put("type", "jp");
            jp.put("name", dictionaryVO.getNameJp());
            jp.put("audio", dictionaryVO.getAudioJp());
            list.add(jp);
            //韩语
            Map<String, Object> kr = new HashMap<>();
            kr.put("type", "kr");
            kr.put("name", dictionaryVO.getNameKr());
            kr.put("audio", dictionaryVO.getAudioKr());
            list.add(kr);
            //葡萄牙语
            Map<String, Object> pt = new HashMap<>();
            pt.put("type", "pt");
            pt.put("name", dictionaryVO.getNamePt());
            pt.put("audio", dictionaryVO.getAudioPt());
            list.add(pt);
            //俄语
            Map<String, Object> ru = new HashMap<>();
            ru.put("type", "ru");
            ru.put("name", dictionaryVO.getNameRu());
            ru.put("audio", dictionaryVO.getAudioRu());
            list.add(ru);
            //西班牙语
            Map<String, Object> es = new HashMap<>();
            es.put("type", "es");
            es.put("name", dictionaryVO.getNameEs());
            es.put("audio", dictionaryVO.getAudioEs());
            list.add(es);
            //英文
            if ("en".equals(type)) {
                Map<String, Object> enMap = list.get(0);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("en");
                dicVO.setName((String) enMap.get("name"));
                dicVO.setAudio((String) enMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(0);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //中文
            if ("cn".equals(type)) {
                Map<String, Object> cnMap = list.get(1);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("cn");
                dicVO.setName((String) cnMap.get("name"));
                dicVO.setAudio((String) cnMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(1);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //阿拉伯语
            if ("ar".equals(type)) {
                Map<String, Object> arMap = list.get(2);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("ar");
                dicVO.setName((String) arMap.get("name"));
                dicVO.setAudio((String) arMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(2);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //法语
            if ("fr".equals(type)) {
                Map<String, Object> frMap = list.get(3);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("fr");
                dicVO.setName((String) frMap.get("name"));
                dicVO.setAudio((String) frMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(3);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //德语
            if ("de".equals(type)) {
                Map<String, Object> deMap = list.get(4);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("de");
                dicVO.setName((String) deMap.get("name"));
                dicVO.setAudio((String) deMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(4);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //意大利语
            if ("it".equals(type)) {
                Map<String, Object> itMap = list.get(5);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("de");
                dicVO.setName((String) itMap.get("name"));
                dicVO.setAudio((String) itMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(5);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //日本语
            if ("jp".equals(type)) {
                Map<String, Object> jpMap = list.get(6);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("jp");
                dicVO.setName((String) jpMap.get("name"));
                dicVO.setAudio((String) jpMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(6);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //韩语
            if ("kr".equals(type)) {
                Map<String, Object> krMap = list.get(7);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("kr");
                dicVO.setName((String) krMap.get("name"));
                dicVO.setAudio((String) krMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(7);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //葡萄牙语
            if ("pt".equals(type)) {
                Map<String, Object> ptMap = list.get(8);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("pt");
                dicVO.setName((String) ptMap.get("name"));
                dicVO.setAudio((String) ptMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(8);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //俄语
            if ("ru".equals(type)) {
                Map<String, Object> ruMap = list.get(9);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("ru");
                dicVO.setName((String) ruMap.get("name"));
                dicVO.setAudio((String) ruMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(9);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
            //西班牙语
            if ("es".equals(type)) {
                Map<String, Object> esMap = list.get(10);
                DicVO dicVO = new DicVO();
                dicVO.setId(dictionaryVO.getId());
                dicVO.setType("es");
                dicVO.setName((String) esMap.get("name"));
                dicVO.setAudio((String) esMap.get("audio"));
                dicVO.setContent(dictionaryVO.getContent());
                dicVO.setCount(dictionaryVO.getCount());
                list.remove(10);
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> map = list.get(j);
                    DictionaryAudioDetailVo dictionaryAudioDetailVoCn = new DictionaryAudioDetailVo();
                    dictionaryAudioDetailVoCn.setType((String) map.get("type"));
                    dictionaryAudioDetailVoCn.setName((String) map.get("name"));
                    dictionaryAudioDetailVoCn.setAudioSrc((String) map.get("audio"));
                    detailVos.add(dictionaryAudioDetailVoCn);
                }
                dicVO.setDetailVos(detailVos);
                result.add(dicVO);
            }
        }
        return result;
    }

    @Override
    public Long selectCount() {
        return mapper.selectCount();
    }

    @Override
    public DictionaryDetailVo initDictionaryDetail(Integer id, String type, String userId) {
        DictionaryDetailVo detailVo = new DictionaryDetailVo();
        //按照ID获取术语对象
        Dictionary dictionary = mapper.findById(id);
        //id不为空,调用日志接口,保存日志,做为行文分析数据使用
        if (id != null && id != 0) {
            if (userId != null) {
                if (dictionary.getClassificationCnId()!=-1){
                    logAnalyseServiceImpl.insertLog(dictionary, type, userId);
                }
            }
        }
        if (dictionary != null) {
            //封装音频对象
            // detailVo.setAudiosList(this.transferAudio(dictionary));
            this.transferAudio(dictionary, detailVo, type);
            //封装术语解读相关
            detailVo.setDetailsList(detailMapper.findByDictionaryIdAndType(id, type));
        }
        return detailVo;
    }

    /**
     * 根据输入查询内容 查询相关术语
     *
     * @param value
     * @return
     */
    @Override
    public List<HashMap<String, Object>> selectDictionaryByValue(String value) {
        List<HashMap<String, Object>> result = Lists.newArrayList();

        //en
        List<Dictionary> enlist = mapper.selectDictionaryByValue("en", value);
        for (int i = 0; i < enlist.size(); i++) {
            Dictionary dictionary = enlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameEn());
            result.add(map);
        }

        List<Dictionary> cnlist = mapper.selectDictionaryByValue("cn", value);
        for (int i = 0; i < cnlist.size(); i++) {
            Dictionary dictionary = cnlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameCn());
            result.add(map);
        }

        List<Dictionary> arlist = mapper.selectDictionaryByValue("ar", value);
        for (int i = 0; i < arlist.size(); i++) {
            Dictionary dictionary = arlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameAr());
            result.add(map);
        }

        List<Dictionary> frlist = mapper.selectDictionaryByValue("fr", value);
        for (int i = 0; i < frlist.size(); i++) {
            Dictionary dictionary = frlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameFr());
            result.add(map);
        }

        List<Dictionary> delist = mapper.selectDictionaryByValue("de", value);
        for (int i = 0; i < delist.size(); i++) {
            Dictionary dictionary = delist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameDe());
            result.add(map);
        }

        List<Dictionary> itlist = mapper.selectDictionaryByValue("it", value);
        for (int i = 0; i < itlist.size(); i++) {
            Dictionary dictionary = itlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameIt());
            result.add(map);
        }

        List<Dictionary> jplist = mapper.selectDictionaryByValue("jp", value);
        for (int i = 0; i < jplist.size(); i++) {
            Dictionary dictionary = jplist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameJp());
            result.add(map);
        }

        List<Dictionary> krlist = mapper.selectDictionaryByValue("kr", value);
        for (int i = 0; i < krlist.size(); i++) {
            Dictionary dictionary = krlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameKr());
            result.add(map);
        }

        List<Dictionary> ptlist = mapper.selectDictionaryByValue("pt", value);
        for (int i = 0; i < ptlist.size(); i++) {
            Dictionary dictionary = ptlist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNamePt());
            result.add(map);
        }

        List<Dictionary> rulist = mapper.selectDictionaryByValue("ru", value);
        for (int i = 0; i < rulist.size(); i++) {
            Dictionary dictionary = rulist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameRu());
            result.add(map);

        }

        List<Dictionary> eslist = mapper.selectDictionaryByValue("es", value);
        for (int i = 0; i < eslist.size(); i++) {
            Dictionary dictionary = eslist.get(i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("dictionaryId", dictionary.getId());
            map.put("value", dictionary.getNameEs());
            result.add(map);
        }
        return result;
    }

    @Override
    public Dictionary selectAudioByName(Integer id, String type) {
        Dictionary dictionary = mapper.findById(id);
        String stringAddress = selectName(type, dictionary);
        return dictionary;
    }

    /**
     * 公用方法 根据传入语言,对象,返回对应术语
     *
     * @param language   语言
     * @param dictionary 术语对象
     * @return
     * @author wph
     */
    public String selectName(String language, Dictionary dictionary) {
        String stringAddress = null;
        switch (language) {
            case "en":
                stringAddress = dictionary.getNameEn();
                break;
            case "cn":
                stringAddress = dictionary.getNameCn();
                break;
            case "ar":
                stringAddress = dictionary.getNameAr();
                break;
            case "fr":
                stringAddress = dictionary.getNameFr();
                break;
            case "de":
                stringAddress = dictionary.getNameDe();
                break;
            case "it":
                stringAddress = dictionary.getNameIt();
                break;
            case "jp":
                stringAddress = dictionary.getNameJp();
                break;
            case "kr":
                stringAddress = dictionary.getNameKr();
                break;
            case "pt":
                stringAddress = dictionary.getNamePt();
                break;
            case "ru":
                stringAddress = dictionary.getNameRu();
                break;
            case "es":
                stringAddress = dictionary.getNameEs();
                break;
        }
        return stringAddress;
    }

    /**
     * 公用方法 根据语种和术语查询对应语种多级分类表ID
     *
     * @param language   语言
     * @param dictionary 术语对象
     * @return int 语种多级分类表ID
     * @Author wph
     */
    public int selectDictionaryId(String language, Dictionary dictionary) {
        int id = 0;
        switch (language) {
            case "en":
                id = dictionary.getClassificationEnId();
                break;
            case "cn":
                id = dictionary.getClassificationCnId();
                break;
            case "ar":
                id = dictionary.getClassificationArId();
                break;
            case "fr":
                id = dictionary.getClassificationFrId();
                break;
            case "de":
                id = dictionary.getClassificationDeId();
                break;
            case "it":
                id = dictionary.getClassificationItId();
                break;
            case "jp":
                id = dictionary.getClassificationJpId();
                break;
            case "kr":
                id = dictionary.getClassificationKrId();
                break;
            case "pt":
                id = dictionary.getClassificationPtId();
                break;
            case "ru":
                id = dictionary.getClassificationRuId();
                break;
            case "es":
                id = dictionary.getClassificationEsId();
                break;
        }
        return id;
    }

    /**
     * 根据术语和语种查询11种语言名称(整个对象)
     *
     * @param
     * @param type 语种
     * @return
     * @author wph
     */
    @Override
    public Dictionary selectAllName(String name, String type) {
        Dictionary professional = mapper.isProfessional(type, name);
        if (!StringUtils.isEmpty(professional)) {
            return professional;
        }
        return null;
    }

    /**
     * 根据术语和语种查询术语定义、参考来源
     *
     * @param name 术语
     * @param type 语种
     * @return
     * @author wph
     */
    @Override
    public List<EntryDetails> selectDefinitionAndSource(String name, String type) {
        Dictionary dictionary = mapper.isProfessional(type, name);
        if (!StringUtils.isEmpty(dictionary)) {
            return detailMapper.findByDictionaryIdAndType(dictionary.getId(), type);
        }
        return null;
    }

//    /**
//     * 根据术语和语种查询知识图谱老方法
//     *
//     * @param type 语种
//     * @param name 术语
//     * @param id   术语id
//     * @return
//     * @author wph
//     */
//    @Override
//    public JSONObject selectKnowledge(String type, String name, Integer id) {
//        JSONObject resultObject = new JSONObject();
//        JSONArray resultArray = new JSONArray();
//        JSONArray levelFiveArr = new JSONArray();
//        resultObject.put("value", name);
//        Dictionary dictionary = mapper.findById(id);
//        Integer classificationId = 0;
//        Dictionary newDictionary = new Dictionary();
//        String similarWords = "";
//        //术语判空
//        if (!ObjectUtils.isEmpty(dictionary)) {
//            List<Map<String, Object>> tempList = new ArrayList<>();
//            //根据语种类型判断术语查询字段
//            switch (type) {
//                case "en":
//                    classificationId = dictionary.getClassificationEnId();
//                    newDictionary.setNameEn(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationEnId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(name);
//                    break;
//                case "cn":
//                    classificationId = dictionary.getClassificationCnId();
//                    newDictionary.setNameCn(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationCnId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(name);
//                    break;
//                case "ar":
//                    classificationId = dictionary.getClassificationArId();
//                    newDictionary.setNameAr(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationArId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "fr":
//                    classificationId = dictionary.getClassificationFrId();
//                    newDictionary.setNameFr(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationFrId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "de":
//                    classificationId = dictionary.getClassificationDeId();
//                    newDictionary.setNameDe(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationDeId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "it":
//                    classificationId = dictionary.getClassificationItId();
//                    newDictionary.setNameIt(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationItId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "jp":
//                    classificationId = dictionary.getClassificationJpId();
//                    newDictionary.setNameJp(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationJpId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "kr":
//                    classificationId = dictionary.getClassificationKrId();
//                    newDictionary.setNameKr(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationKrId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "pt":
//                    classificationId = dictionary.getClassificationPtId();
//                    newDictionary.setNamePt(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationPtId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "ru":
//                    classificationId = dictionary.getClassificationRuId();
//                    newDictionary.setNameRu(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationRuId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = getSimilarWords(dictionary.getNameEn());
//                    break;
//                case "es":
//                    classificationId = dictionary.getClassificationEsId();
//                    newDictionary.setNameEs(name);
//                    if (classificationId != -1) {
//                        newDictionary.setClassificationEsId(classificationId);
//                        tempList = mapper.selectFourDictionary(newDictionary);
//                    }
//                    similarWords = this.getSimilarWords(dictionary.getNameEn());
//                    break;
//            }
//            //四级术语添加
//            if (tempList != null && !tempList.isEmpty()) {
//                tempList.forEach(itme -> {
//                    JSONObject jsonObject = new JSONObject();
//                    jsonObject.put("value", itme.get("name"));
//                    jsonObject.put("dictionaryId", itme.get("id"));
//                    resultArray.add(jsonObject);
//                });
//            }
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("value", name);
//            jsonObject.put("dictionaryId", id);
//            resultArray.add(jsonObject);
//            resultObject.put("levelFourArr", resultArray);
//            //原有一二三级术语添加
//            if (classificationId != -1) {
//                Classification classification = classificationMapper.selectByPrimaryKey(classificationId);
//                resultObject.put("levelOne", classification.getFirstType());
//                resultObject.put("levelTwo", classification.getSecondType());
//                resultObject.put("levelThree", classification.getThirdType());
//            }
//            //循环调用四级术语去腾讯nlp获取同义词
//            JSONObject levelFiveObject = JSONObject.parseObject(similarWords);
//            if (levelFiveObject != null && levelFiveObject.size() > 0) {
//                JSONArray similarWordsArray = levelFiveObject.getJSONArray("SimilarWords");
//                if (similarWordsArray != null && similarWordsArray.size() > 0) {
//                    circulation:
//                    for (int i = 0; i < similarWordsArray.size(); i++) {
//                        Object item = similarWordsArray.get(i);
//                        //两次for循环判断4级和腾讯查出的5级中没有重复词,有则删除
//                        for (int k = 0; k < resultArray.size(); k++) {
//                            JSONObject jsonObject1 = (JSONObject) resultArray.get(k);
//                            Object comparisonFour = jsonObject1.get("value");
//                            if (comparisonFour.equals(item)) {
//                                similarWordsArray.remove(item);
//                                continue circulation;
//                            }
//                        }
//                        //判断非汉语,调用工具类 去除中文
//                        if (!"cn".equals(type)) {
//                            if (!tool.isContainChinese(item.toString())) {
//                                levelFiveArr.add(item);
//                            }
//                        } else {
//                            levelFiveArr.add(item);
//                        }
//                        if (levelFiveArr.size() >= 3) {
//                            break circulation;
//                        }
//                    }
//                }
//            }
//            resultObject.put("levelFiveArr", levelFiveArr);
//            return resultObject;
//        } else {
//            return null;
//        }
//    }

    /**
     * 根据术语和语种查询知识图谱 新方法
     *
     * @param type 语种
     * @param name 术语
     * @param id   术语id
     * @return
     * @author wph
     */
    public JSONObject selectNewKnowledge(String type, String name, Integer id) {
        JSONObject resultObject = new JSONObject();
        Dictionary dictionary = mapper.findById(id);
        //术语判空
        if (!ObjectUtils.isEmpty(dictionary)) {
            String code = dictionary.getCode();
            if (code != null && !"".equals(code)) {
                int length = code.split("\\.").length;
                //一级分类时
                if (length == 1 && code.length() == 1) {
                    resultObject = oneLevel(dictionary, type, name);
                } else if (length == 2) {
                    //二级分类时
                    resultObject = twoLevel(dictionary, type, name);
                } else if (length == 3) {
                    //三级分类时
                    resultObject = threeLevel(dictionary, type, name);
                } else {
                    //四级分类时
                    resultObject = fourLevel(dictionary, type, name, id);
                }
            }
            return resultObject;
        } else {
            return null;
        }
    }

    private JSONObject fourLevel(Dictionary dictionary, String type, String name, Integer id) {
        List<Map<String, Object>> tempList = new ArrayList<>();
        JSONArray resultArray = new JSONArray();
        JSONArray levelFiveArr = new JSONArray();
        Integer classificationId = 0;
        Dictionary newDictionary = new Dictionary();
        String tempSimilarWords = "";
        JSONObject resultObject = new JSONObject();
        //根据语种类型判断术语查询字段
        switch (type) {
            case "en":
                classificationId = dictionary.getClassificationEnId();
                newDictionary.setNameEn(name);
                newDictionary.setClassificationEnId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = name;
                break;
            case "cn":
                classificationId = dictionary.getClassificationCnId();
                newDictionary.setNameCn(name);
                newDictionary.setClassificationCnId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = name;
                break;
            case "ar":
                classificationId = dictionary.getClassificationArId();
                newDictionary.setNameAr(name);
                newDictionary.setClassificationArId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "fr":
                classificationId = dictionary.getClassificationFrId();
                newDictionary.setNameFr(name);
                newDictionary.setClassificationFrId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "de":
                classificationId = dictionary.getClassificationDeId();
                newDictionary.setNameDe(name);
                newDictionary.setClassificationDeId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "it":
                classificationId = dictionary.getClassificationItId();
                newDictionary.setNameIt(name);
                newDictionary.setClassificationItId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "jp":
                classificationId = dictionary.getClassificationJpId();
                newDictionary.setNameJp(name);
                newDictionary.setClassificationJpId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "kr":
                classificationId = dictionary.getClassificationKrId();
                newDictionary.setNameKr(name);
                newDictionary.setClassificationKrId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "pt":
                classificationId = dictionary.getClassificationPtId();
                newDictionary.setNamePt(name);
                newDictionary.setClassificationPtId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "ru":
                classificationId = dictionary.getClassificationRuId();
                newDictionary.setNameRu(name);
                newDictionary.setClassificationRuId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
            case "es":
                classificationId = dictionary.getClassificationEsId();
                newDictionary.setNameEs(name);
                newDictionary.setClassificationEsId(classificationId);
                tempList = mapper.selectFourDictionary(newDictionary);
                tempSimilarWords = dictionary.getNameEn();
                break;
        }
        //四级术语添加
        if (tempList != null && !tempList.isEmpty()) {
            tempList.forEach(itme -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", itme.get("name"));
                jsonObject.put("dictionaryId", itme.get("id"));
                resultArray.add(jsonObject);
            });
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("value", name);
        jsonObject.put("dictionaryId", id);
        resultArray.add(jsonObject);
        resultObject.put("levelFourArr", resultArray);
        //原有一二三级术语添加
        Classification classification = classificationMapper.selectByPrimaryKey(classificationId);

        Dictionary tempDictionaryOne = mapper.isProfessional(type, classification.getFirstType());
        JSONObject jsonObjectOne = new JSONObject();
        jsonObjectOne.put("value",classification.getFirstType());
        jsonObjectOne.put("dictionaryId",tempDictionaryOne.getId());
        resultObject.put("levelOne", jsonObjectOne);

        Dictionary tempDictionaryTwo = mapper.isProfessional(type, classification.getSecondType());
        JSONObject jsonObjectTwo = new JSONObject();
        jsonObjectTwo.put("value",classification.getSecondType());
        jsonObjectTwo.put("dictionaryId",tempDictionaryTwo.getId());
        resultObject.put("levelTwo", jsonObjectTwo);

        Dictionary tempDictionaryThree = mapper.isProfessional(type, classification.getThirdType());
        JSONObject jsonObjectThree = new JSONObject();
        jsonObjectThree.put("value",classification.getThirdType());
        jsonObjectThree.put("dictionaryId",tempDictionaryThree.getId());
        resultObject.put("levelThree", jsonObjectThree);

        //循环调用四级术语去腾讯nlp获取同义词作为五级词
        String similarWords = "";
        similarWords = getSimilarWords(tempSimilarWords);
        JSONObject levelFiveObject = JSONObject.parseObject(similarWords);
        if (levelFiveObject != null && levelFiveObject.size() > 0) {
            JSONArray similarWordsArray = levelFiveObject.getJSONArray("SimilarWords");
            if (similarWordsArray != null && similarWordsArray.size() > 0) {
                circulation:
                for (int i = 0; i < similarWordsArray.size(); i++) {
                    Object item = similarWordsArray.get(i);
                    //两次for循环判断4级和腾讯查出的5级中没有重复词,有则删除
                    for (int k = 0; k < resultArray.size(); k++) {
                        JSONObject jsonObject1 = (JSONObject) resultArray.get(k);
                        Object comparisonFour = jsonObject1.get("value");
                        if (comparisonFour.equals(item)) {
                            similarWordsArray.remove(item);
                            continue circulation;
                        }
                    }
                    //判断非汉语,调用工具类 去除中文
                    if (!"cn".equals(type)) {
                        if (!tool.isContainChinese(item.toString())) {
                            levelFiveArr.add(item);
                        }
                    } else {
                        levelFiveArr.add(item);
                    }
                    if (levelFiveArr.size() >= 3) {
                        break circulation;
                    }
                }
            }
        }
        resultObject.put("value", name);
        resultObject.put("levelFiveArr", levelFiveArr);
        //当前术语类别为4
        resultObject.put("type", 4);
        return resultObject;
    }

    private JSONObject threeLevel(Dictionary dictionary, String type, String name) {
        JSONObject resultObject = new JSONObject();
        JSONArray fourArray = new JSONArray();
        //添加当前三级
        JSONObject threeObject = new JSONObject();
        threeObject.put("value", name);
        threeObject.put("dictionaryId", dictionary.getId());
        resultObject.put("levelThree", threeObject);
        //调用公用方法查询classification表中一二分类
        Classification classification = selectClassificationByNameAndType(dictionary, type);
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        if (!ObjectUtils.isEmpty(classification)) {
            Dictionary levelOneDictionary = mapper.isProfessional(type, classification.getFirstType());
            jsonObject1.put("dictionaryId", levelOneDictionary.getId());
            jsonObject1.put("value", classification.getFirstType());
            resultObject.put("levelOne", jsonObject1);

            Dictionary levelTwoDictionary = mapper.isProfessional(type, classification.getSecondType());
            jsonObject2.put("dictionaryId", levelTwoDictionary.getId());
            jsonObject2.put("value", classification.getSecondType());
            resultObject.put("levelTwo", jsonObject2);
            resultObject.put("value", classification.getThirdType());
        }
        //查询四级分类
        Dictionary newDictionary = new Dictionary();
        Integer classificationId = 0;
        List<Map<String, Object>> tempList = new ArrayList<>();
        //根据语种类型判断术语查询字段
        switch (type) {
            case "en":
                classificationId = dictionary.getClassificationEnId();
                newDictionary.setNameEn(name);
                newDictionary.setClassificationEnId(classificationId);
                break;
            case "cn":
                classificationId = dictionary.getClassificationCnId();
                newDictionary.setNameCn(name);
                newDictionary.setClassificationCnId(classificationId);
                break;
            case "ar":
                classificationId = dictionary.getClassificationArId();
                newDictionary.setNameAr(name);
                newDictionary.setClassificationArId(classificationId);
                break;
            case "fr":
                classificationId = dictionary.getClassificationFrId();
                newDictionary.setNameFr(name);
                newDictionary.setClassificationFrId(classificationId);
                break;
            case "de":
                classificationId = dictionary.getClassificationDeId();
                newDictionary.setNameDe(name);
                newDictionary.setClassificationDeId(classificationId);
                break;
            case "it":
                classificationId = dictionary.getClassificationItId();
                newDictionary.setNameIt(name);
                newDictionary.setClassificationItId(classificationId);
                break;
            case "jp":
                classificationId = dictionary.getClassificationJpId();
                newDictionary.setNameJp(name);
                newDictionary.setClassificationJpId(classificationId);
                break;
            case "kr":
                classificationId = dictionary.getClassificationKrId();
                newDictionary.setNameKr(name);
                newDictionary.setClassificationKrId(classificationId);
                break;
            case "pt":
                classificationId = dictionary.getClassificationPtId();
                newDictionary.setNamePt(name);
                newDictionary.setClassificationPtId(classificationId);
                break;
            case "ru":
                classificationId = dictionary.getClassificationRuId();
                newDictionary.setNameRu(name);
                newDictionary.setClassificationRuId(classificationId);
                break;
            case "es":
                classificationId = dictionary.getClassificationEsId();
                newDictionary.setNameEs(name);
                newDictionary.setClassificationEsId(classificationId);
                break;
        }
        tempList = mapper.selectFourDictionaryOnNegative(newDictionary);
        //四级术语添加
        if (tempList != null && !tempList.isEmpty()) {
            tempList.forEach(itme -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", itme.get("name"));
                jsonObject.put("dictionaryId", itme.get("id"));
                fourArray.add(jsonObject);
            });
        }
        resultObject.put("levelFourArr", fourArray);
        //当前术语类别为3
        resultObject.put("type", 3);
        return resultObject;
    }

    /**
     * 当传入的术语为二级术语时
     *
     * @param dictionary 对象
     * @param type       术语语种类型 en cn ar fr de it jp kr pt ru es
     * @param name       术语名字
     * @return
     */
    private JSONObject twoLevel(Dictionary dictionary, String type, String name) {
        JSONObject resultObject = new JSONObject();
        JSONArray threeArray = new JSONArray();
        //查询三级分类
        List<Map<String, Object>> threeList = classificationMapper.selectLevelThreeClassification(name);
        if (!ObjectUtils.isEmpty(threeList) && threeList.size() > 0) {
            for (int i = 0; i < threeList.size(); i++) {
                Dictionary tempDictionary = mapper.isProfessional(type, threeList.get(i).get("third_type").toString());
                if (!ObjectUtils.isEmpty(tempDictionary)) {
                    JSONObject tempObject = new JSONObject();
                    tempObject.put("dictionaryId", tempDictionary.getId());
                    tempObject.put("value", threeList.get(i).get("third_type"));
                    threeArray.add(tempObject);
                }
            }
            resultObject.put("levelThree", threeArray);
        }
        //调用公用方法查询classification表中一二分类
        Classification classification = selectClassificationByNameAndCode(dictionary, type);
        JSONObject levelOneObject = new JSONObject();
        JSONObject levelTwoObject = new JSONObject();
        if (!ObjectUtils.isEmpty(classification)) {
            Dictionary tempDictionaryOne = mapper.isProfessional(type, classification.getFirstType());
            if (!ObjectUtils.isEmpty(tempDictionaryOne)) {
                levelOneObject.put("dictionaryId", tempDictionaryOne.getId());
                levelOneObject.put("value", classification.getFirstType());
            }
            resultObject.put("levelOne", levelOneObject);

            Dictionary tempDictionaryTwo = mapper.isProfessional(type, classification.getSecondType());
            if (!ObjectUtils.isEmpty(tempDictionaryOne)) {
                levelTwoObject.put("dictionaryId", tempDictionaryTwo.getId());
                levelTwoObject.put("value", classification.getSecondType());
            }
            resultObject.put("levelTwo", levelTwoObject);
            resultObject.put("value", classification.getSecondType());
        }
        //当前术语类别为2
        resultObject.put("type", 2);
        return resultObject;
    }

    /**
     * 根据code和术语语种类型来查询一级术语和二级术语(本身)
     *
     * @param dictionary
     * @param type
     * @return
     */
    private Classification selectClassificationByNameAndCode(Dictionary dictionary, String type) {
        String code = dictionary.getCode();
        //根据语种类型判断术语查询字段
        Classification classification = classificationMapper.selectByTwoCodeAndType(code, type);
        return classification;
    }

    /**
     * 根据类型和名字查询classificationId后获取对应classification对象
     */
    private Classification selectClassificationByNameAndType(Dictionary dictionary, String type) {
        String code = dictionary.getCode();
        //根据语种类型判断术语查询字段
        Classification classification = classificationMapper.selectByThreeCodeAndType(code, type);
        return classification;
    }

    /**
     * 当传入的术语为一级术语时
     *
     * @param dictionary 对象
     * @param type       术语语种类型 en cn ar fr de it jp kr pt ru es
     * @param name       术语名字
     * @return
     */
    private JSONObject oneLevel(Dictionary dictionary, String type, String name) {
        JSONObject resultObject = new JSONObject();
        JSONObject oneObject = new JSONObject();
        JSONArray twoArray = new JSONArray();
        //查询一级术语同级的3个一级术语
        List<IndexDictionaryVo> listDictionary = mapper.selectLevelOneDictionary(type, name);
        oneObject.put("value", name);
        oneObject.put("dictionaryId", dictionary.getId());
        resultObject.put("levelOne", oneObject);
        //查询一级术语下面的3个二级术语
        List<Map<String, Object>> twoDictionary = classificationMapper.selectLevelTwoClassification(name);
        if (!ObjectUtils.isEmpty(twoDictionary) && twoDictionary.size() > 0) {
            for (int i = 0; i < twoDictionary.size(); i++) {
                Dictionary tempDictionary = mapper.selectTwoLevelDictionary(type, twoDictionary.get(i).get("second_type").toString());
                if (!ObjectUtils.isEmpty(tempDictionary)) {
                    JSONObject tempObject = new JSONObject();
                    tempObject.put("dictionaryId", tempDictionary.getId());
                    tempObject.put("value", twoDictionary.get(i).get("second_type"));
                    twoArray.add(tempObject);
                }
            }
            resultObject.put("levelTwo", twoArray);
        }
        resultObject.put("value", name);
        //当前术语类别为1
        resultObject.put("type", 1);
        return resultObject;
    }

    /**
     * 获取腾讯nlp同义词value
     *
     * @param keyword
     * @return
     */
    public String getSimilarWords(String keyword) {
        //公司申请账户
        String txCloud_secretId = "AKIDRkxTtAvM9gzWhi515HDvUuStnvoRRfvx";
        String txCloud_secretKey = "CdolTfbIjssnjSogJZ4LOFrKce3c1DAv";
        String result = "";
        if (!StringUtils.isEmpty(keyword)) {
            if (keyword.length() > 20) {
                keyword = keyword.replaceAll("<|>|《|》|（|）|“|”| |/|", "");
                keyword = keyword.substring(0, 20);
            }
            Credential cred = new Credential(txCloud_secretId, txCloud_secretKey);
            HttpProfile httpProfile = new HttpProfile();
            //地域参数
            httpProfile.setEndpoint("nlp.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            NlpClient client = new NlpClient(cred, "ap-guangzhou", clientProfile);
            String params = "{\"Text\":\"" + keyword + "\",\"WordNumber\":20}";
            try {

                SimilarWordsRequest req = SimilarWordsRequest.fromJsonString(params, SimilarWordsRequest.class);
                SimilarWordsResponse resp = client.SimilarWords(req);
                result = SimilarWordsRequest.toJsonString(resp);
            } catch (TencentCloudSDKException e) {
                log.info("腾讯云nlp同义词接口调用失败，----" + keyword);
            }
        }
        return result;
    }

    /**
     * 延伸阅读
     * 根据术语名称查询对应图书和论文
     *
     * @param name 术语名字
     * @author wph
     */
    @Override
    public JSONObject selectBookArtical(String name) {
        List<Map<String, Object>> bookList = relateBookMapper.selectListByName(name);
        List<Map<String, Object>> journalArticalList = relateJournalArticalMapper.selectListByName(name);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("book", bookList);
        jsonObject.put("journalArtical", journalArticalList);
        return jsonObject;
    }

    void transferAudio(Dictionary dictionary, DictionaryDetailVo detailVo, String type) {
        List<DictionaryAudioDetailVo> audioDetailVos = Lists.newArrayList();
        Arrays.stream(languageType).forEach(r -> {
            DictionaryAudioDetailVo vo = new DictionaryAudioDetailVo();
            vo.setId(dictionary.getId());
            vo.setType(r);
            if ("en".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioEn());
                vo.setName(dictionary.getNameEn());
            } else if ("cn".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioCn());
                vo.setName(dictionary.getNameCn());
            } else if ("ar".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioAr());
                vo.setName(dictionary.getNameAr());
            } else if ("fr".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioFr());
                vo.setName(dictionary.getNameFr());
            } else if ("de".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioDe());
                vo.setName(dictionary.getNameDe());
            } else if ("it".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioIt());
                vo.setName(dictionary.getNameIt());
            } else if ("jp".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioJp());
                vo.setName(dictionary.getNameJp());
            } else if ("kr".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioKr());
                vo.setName(dictionary.getNameKr());
            } else if ("pt".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioPt());
                vo.setName(dictionary.getNamePt());

            } else if ("ru".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioRu());
                vo.setName(dictionary.getNameRu());

            } else if ("es".equalsIgnoreCase(r)) {
                vo.setAudioSrc(dictionary.getAudioEs());
                vo.setName(dictionary.getNameEs());
            } else {
                //默认就是英文
                vo.setAudioSrc(dictionary.getAudioEn());
                vo.setName(dictionary.getNameEn());
            }
            if (type.equalsIgnoreCase(r)) {
                detailVo.setName(vo.getName());
            }
            audioDetailVos.add(vo);
        });
        detailVo.setAudiosList(audioDetailVos);
    }
}

